#!/bin/bash
#@author: shevchenko
#date: 2016.4.5
#ver:  v0.0.1
#description:  安装三国挂机游戏区服,基于现有镜像，其他场景不适用,请注意.
#usage:  ./install_sanguoguaji.sh --SLAN=BJ --SNUM=12

set -o nounset

#增加操作锁,防止脚本同一个时间执行两次
LOCKFILE=/tmp/$(basename $0)_lockfile
if [ -f $LOCKFILE  ];then
   MYPID=$(cat $LOCKFILE)
   ps -p $MYPID | grep $MYPID &>/dev/null
   [ $? -eq 0 ] && echo "The script is busy..." && exit 1
else
   echo $$ > $LOCKFILE
fi


#获取公网IP
IPS=`curl -s icanhazip.com`

#限时商店时间设定
YEAR=`date "+%Y"`
TYEAR=`date -d "tomorrow" +"%Y"`
THREEYEAR=`date +%Y --date='4 day'`
MONTH=`date "+%m"`
TMONTH=`date -d "tomorrow" +"%m"`
THREEMONTH=`date +%m --date='4 day'`
DAY=`date "+%d"`
TDAY=`date -d "tomorrow" +"%d"`
THREETDAY=`date +%d --date='4 day'`


usage () {
cat <<EOF
-------------------------------------------------------------------------------
    Usage: $0 [OPTIONS]
    注意如下选项都是必填项!
    --SLAN=EN       安装语言版本：TW台湾，BJ北京
    --SNUM=11412       区服版本
    如安装北京12服，执行方式为：sh install_sanguoguaji.sh  --SLAN=BJ --SNUM=11412
-------------------------------------------------------------------------------
EOF
    exit 1
}


#处理脚本传参
check_arguments() {
    for arg do
        case "$arg" in
        --SLAN=*)
            SLAN=`echo "$arg" | sed -e "s;--SLAN=;;"` ;;
        --SNUM=*)
            SNUM=`echo "$arg" | sed -e "s;--SNUM=;;"` ;;
        *)
            echo -e "[\033[1;31m 输入错误!! \033[0m]"
            usage;;
        esac
    done

    if [ $# != 2 ] ; then
    	  echo -e "[\033[1;31m 参数数量不正确 \033[0m]"
        usage
    fi

    if [ -z "${SLAN}" ] || [ -z "${SNUM}" ];then
			  echo -e "[\033[1;31m 安装语言和区服不能为空 \033[0m]"
			  usage
	  fi

	  #判断区服
    if ( echo ${SNUM} |grep -E '[^0-9]' >/dev/null 2>&1 );then
        echo -e "[\033[1;31m 区服不是纯数字,请检查 \033[0m]"
        usage
    fi

    #判断语言版本
    if ! `echo ${SLAN} |egrep -w "BJ|TW" >/dev/null 2>&1`;then
        echo -e "[\033[1;31m 输入区服地区不是北京或台湾 \033[0m]"
        usage
    fi
}


#初始化系统配置
init_system() {
    local SLAN=$1
    local SNUM=$2

    #修改主机名
    OLOHOSTNAME=`grep "HOSTNAME" /etc/sysconfig/network`
    LLANG=`echo ${SLAN} | tr A-Z a-z`
    NEWHOSTNAME="${LLANG}-guaji-${SNUM:3}"
    sed -i "s/${OLOHOSTNAME}/HOSTNAME=${NEWHOSTNAME}/g" /etc/sysconfig/network
    echo "export LANG=en_US.UTF-8" >> /root/.bashrc
    echo "export PS1='[\[\e[36;1m\]\u@\[\e[32;1m\]${NEWHOSTNAME}-${IPS}\[\e[31;1m\] \t\[\e[36;1m\]\w\[\e[0m\]]# '" >> /root/.bashrc

    #配置防火墙
    chkconfig --level 345 iptables on

    iptables=/sbin/iptables
    $iptables -F
    $iptables -X
    $iptables -P INPUT  DROP
    $iptables -P OUTPUT ACCEPT
    $iptables -P FORWARD DROP
    $iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    $iptables -A INPUT -p icmp -j ACCEPT
    $iptables -A INPUT -i lo -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 17751 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 17713 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 17777 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 17710 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 17799 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 17711 -j ACCEPT
    $iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    $iptables -A INPUT -s 54.64.32.219 -m state --state NEW -m tcp -p tcp --dport 10050  -j ACCEPT
    /etc/rc.d/init.d/iptables save

    #修改监控
    service monit stop
    sed -i "s/`grep "system" \/etc\/monit.d\/system.monit`/system ${NEWHOSTNAME}/g" /etc/monit.d/system.monit
    sed -i "s/`grep "mysql/" \/etc\/monit.d\/mysql.monit`/mysql/ ${NEWHOSTNAME}/g" /etc/monit.d/mysql.monit
    rm /etc/monit.d/tsb.monit

    #
    #修改git源
    sed -i "s/`grep "port" \/root\/.ssh\/config`/  port 22/g"  /root/.ssh/config
    sed -i "s/`grep "hostname" \/root\/.ssh\/config`/  hostname 123.59.65.86/g"  /root/.ssh/config

    #初始化安装数据库
    mkdir -p /data/lib/mysql
    chown -R mysql:mysql /data/lib/mysql
    cd /data
    /usr/bin/mysql_install_db
    [ $? -ne 0 ] && echo -e "[\033[1;31m 数据库初始化失败,请检查 \033[0m]"  && exit 1
    sed -i "12i\max_allowed_packet = 10M" /etc/my.cnf
    service mysql start
}


#配置游戏区服
install_game() {
    local SLAN=$1
    local SNUM=$2

    #git拉资源
    cd /data
    git clone ucloud:game/server/pikachu-res.git
    cd /data/pikachu-res
    #切换分支
    LLANG=`echo ${SLAN} | tr A-Z a-z`
    git co guaji-${LLANG}-res
    git st

    #建立备份
    mkdir -p  /data/bak/mysql_backup/all_backup
    cd /data/bak/mysql_backup/
    cp /data/pikachu-res/game/script/all_backup.sh /data/bak/mysql_backup/
    mv all_backup.sh solo_backup.sh
    chmod u+x solo_backup.sh

    #创建游戏数据库
    /usr/bin/mysql -e "show databases" | egrep -w "solo" >/dev/null 2>&1 && echo -e "[\033[1;31m 存在同名数据库,请检查 \033[0m]"  && exit 1
    /usr/bin/mysql -e "CREATE DATABASE \`solo\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
    /usr/bin/mysql solo < /data/pikachu-res/db/dxjh_db_create_head.sql
    /usr/bin/mysql -e "update solo.t_var set maxcharguid=${SNUM}00000001 where maxcharguid=1000100000001;"

    #修改配置文件
    sed -i "s/`grep "Year" \/data\/pikachu-res\/game\/server\/settings\/gametoggleconfig.ini`/Year=${YEAR}/g" /data/pikachu-res/game/server/settings/gametoggleconfig.ini
    sed -i "s/`grep "Month" \/data\/pikachu-res\/game\/server\/settings\/gametoggleconfig.ini`/Month=${MONTH}/g" /data/pikachu-res/game/server/settings/gametoggleconfig.ini
    sed -i "s/`grep "Day" \/data\/pikachu-res\/game\/server\/settings\/gametoggleconfig.ini`/Day=${DAY}/g" /data/pikachu-res/game/server/settings/gametoggleconfig.ini
    sed -i '13,14d' /data/pikachu-res/game/server/settings/gametoggleconfig.ini
    echo "BeginTime=${TYEAR}/${TMONTH}/${TDAY}/16/0" >>/data/pikachu-res/game/server/settings/gametoggleconfig.ini
    echo "EndTime=${THREEYEAR}/${THREEMONTH}/${THREETDAY}/16/0" >>/data/pikachu-res/game/server/settings/gametoggleconfig.ini

    sed -i "s/`grep "GLServerID" \/data\/pikachu-res\/game\/server\/settings\/worldindex.ini`/GLServerID=${SNUM}/g" /data/pikachu-res/game/server/settings/worldindex.ini

    \cp -rp /data/pikachu-res/game/server/settings/gametoggleconfig.ini /data/pikachu-res/game/server-tsw/settings
    \cp -rp /data/pikachu-res/game/server/settings/worldindex.ini /data/pikachu-res/game/server-tsw/settings

    # #启动游戏
    # cd /data/pikachu-res/game/script
    # /data/pikachu-res/game/script/pikachu-start.sh
}

###---------------------------------------------------------分割线--------------------------------------------------------------

#必须root用户执行安装
[ `id -u` -ne 0 ] && echo -e "[\033[1;31m 非root用户,脚本退出 \033[0m]" && exit 1

#参数检查
check_arguments "$@"

echo "区服信息如下："
echo -e "语言版本: [\033[1;32m ${SLAN} \033[0m]"
echo -e "游戏区服: [\033[1;32m ${SNUM} \033[0m]"

read -t 10 -p "请确认上述信息是否正确(Y/N),10秒不输入脚本默认退出: "  ANSWER
ANSWER=${ANSWER:-NO}
if [ "$ANSWER" != Y ] && [ $ANSWER != y ] && [ $ANSWER != YES ] && [ $ANSWER != yes ] ; then
	  echo -e "[\033[1;31m 脚本退出,请重新运行脚本 \033[0m]"
    usage
fi


#执行系统初始化和安装游戏区服
init_system ${SLAN} ${SNUM}

install_game ${SLAN} ${SNUM}

#启动监控
service monit restart

echo -e "[\033[1;31m 安装完成,限时商店开启的时候和区服开启的具体时间有关,请确认下限时商店的日期是否正确 \033[0m]"


#删除操作锁
rm -f $LOCKFILE
